package it.polimi.digitalstate.entities;

import it.polimi.digitalstate.dto.MembershipDTO;
import it.polimi.digitalstate.entities.Citizen;
import it.polimi.digitalstate.entities.Party;
import it.polimi.digitalstate.manager.DTOEntityTransformer;

import java.io.Serializable;
import java.sql.Date;

import javax.persistence.*;

/**
 * Entity implementation class for Entity: Membership
 *
 */
@Entity
@NamedQueries(value = { 
		@NamedQuery(name = "membership_getByCitizenID", query = "select m from Membership m where m.member = :memberid and m.date_of_leaving = NULL" ),
		@NamedQuery(name = "membership_getByID", query = "select m from Membership m where m.id = :id" ),
		@NamedQuery(name = "membership_getAllMemberFromParty", query = "select m from Membership m where m.party=:party and m.date_of_leaving = NULL" ),
		@NamedQuery(name = "membership_getNumberofcurrentMembersFromParty", query = "select count(m) from Membership m where m.party=:party and m.date_of_leaving = NULL" ),	
		@NamedQuery(name = "membership_getByCitizen", query = "select m from Membership m where m.member = :member and m.date_of_leaving = NULL" )})
public class Membership implements Serializable , IEntity {
	//Here we don't need more unique constraint 'cause it is a history table (i think in the design document the key {member, party, date_of_joining} is uncorrect...)
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;
	private Citizen member;
	@JoinColumn(nullable = false)
	private Party party;
	@Column(nullable = false)
	private String cv;
	private Date date_of_joining;
	private Date date_of_leaving;
	private static final long serialVersionUID = 1L;

	public Membership() {
		super();
	}   
	public Membership( Citizen member, Party party, String cv, Date date_of_joining, Date date_of_leaving){
		super();
		this.member=member;
		this.party=party;
		this.cv = cv;
		this.date_of_joining= date_of_joining;
		this.date_of_leaving = date_of_leaving;
	}
	public Membership(Party party, Citizen member, MembershipDTO dto){
		super();
		this.member=member;
		this.party=party;
		cv=dto.getCv();
		date_of_joining=new Date( dto.getDate_of_joining().getTime());
	}
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}   
	public Citizen getMember() {
		return this.member;
	}

	public void setMember(Citizen member) {
		this.member = member;
	}   
	public Party getParty() {
		return this.party;
	}

	public void setParty(Party party) {
		this.party = party;
	}   
	public Date getDate_of_joining() {
		return this.date_of_joining;
	}

	public void setDate_of_joining(Date date_of_joining) {
		this.date_of_joining = date_of_joining;
	}   
	public Date getDate_of_leaving() {
		return this.date_of_leaving;
	}

	public void setDate_of_leaving(Date date_of_leaving) {
		this.date_of_leaving = date_of_leaving;
	}
	@Override
	public MembershipDTO getDTO() {
		return new MembershipDTO(id,member.getDTO(),party.getDTO(),cv, date_of_joining, date_of_leaving);
	}
	public String getCv() {
		return cv;
	}
	public void setCv(String cv) {
		this.cv = cv;
	}
}
